$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'service/serviceregister/list',
        datatype: "json",
        colModel: [			
			{ label: '售后订单编号', name: 'srCode', index: 'sr_code', width: 130,
                formatter: function (value, options, row) {
                    return '<a href="javascript:;" onclick="return vm.getInfo(\''+ row.srId +'\');">'+ row.srCode +'</a></li>';
                }
            },
			{ label: '客户网名', name: 'srName', index: 'sr_name', width: 80 }, 			
			{ label: '销售订单号', name: 'srOrderId', index: 'sr_order_id', width: 100 },
			{ label: '配送单号', name: 'srOrderSend', index: 'sr_order_send', width: 80 }, 			
			{ label: '售后类型', name: 'srTypeText', index: 'sr_type', width: 80 },
			{ label: '售后订单状态', name: 'srStatusText', index: 'sr_status', width: 80 },
			{ label: '收货状态', name: 'srReceiveStatusText', index: 'sr_receive_status', width: 80 },
			{ label: '发货状态', name: 'srSendStatusText', index: 'sr_send_status', width: 80 },
			{ label: '售后原因', name: 'srReason', index: 'sr_reason',
                formatter: function (value, options, row) {
                    return row.srReason + (row.srOtherReason == null? '':row.srOtherReason);
                }
            },
			{ label: '转销售订单号', name: 'srShiftOrder', index: 'sr_shift_order', width: 100 },
			/*{ label: '收件人姓名', name: 'srReceiveName', index: 'sr_receive_name', width: 80 },
			{ label: '收件人电话', name: 'srReceiveMobile', index: 'sr_receive_mobile', width: 80 }, 			
			{ label: '收件人所在省份', name: 'srReceiveProvice', index: 'sr_receive_provice', width: 80 }, 			
			{ label: '收件人所在州市', name: 'srReceiveCity', index: 'sr_receive_city', width: 80 }, 			
			{ label: '收件人所在区/县', name: 'srReceiveArea', index: 'sr_receive_area', width: 80 }, 			
			{ label: '收件人所在详细地址', name: 'srReceiveAddress', index: 'sr_receive_address', width: 80 }, 			
			{ label: '收件地区编码', name: 'srReceiveAreacode', index: 'sr_receive_areaCode', width: 80 }, 			
			{ label: '收货邮单号', name: 'srReceivePostalCode', index: 'sr_receive_postal_code', width: 80 }, 			
			{ label: '签收时间', name: 'srSignTime', index: 'sr_sign_time', width: 80 }, 			
			{ label: '签收人', name: 'srSignName', index: 'sr_sign_name', width: 80 }, 			
			{ label: '发货方式', name: 'srSendType', index: 'sr_send_type', width: 80 }, 			
			{ label: '发货时间', name: 'srSendTime', index: 'sr_send_time', width: 80 }, 			
			{ label: '收款时间', name: 'srReceiptTime', index: 'sr_receipt_time', width: 80 }, 			
			{ label: '退款时间', name: 'srRefundTime', index: 'sr_refund_time', width: 80 },
			{ label: '检测人', name: 'srCheckName', index: 'sr_check_name', width: 80 },
			{ label: '检测时间', name: 'srCheckTime', index: 'sr_check_time', width: 80 }, 			
			{ label: '售后完成时间', name: 'srFinishTime', index: 'sr_finish_time', width: 80 }, 			
			{ label: '备注', name: 'srRemark', index: 'sr_remark', width: 80 }, 	*/
			{ label: '更新人', name: 'srUpdateNameStr', index: 'srUpdateNameStr', width: 80 },
			{ label: '更新时间', name: 'srUpdateTime', index: 'sr_update_time', width: 80 },
			{ label: '创建人', name: 'srCreateNameStr', index: 'srCreateNameStr', width: 80 },
			{ label: '创建时间', name: 'srCreateTime', index: 'sr_create_time', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true, // 列表与编辑显示
        dialogResponsibilityVisible: false,// 售后责任弹窗
		title: null,
		serviceRegister: {}, // 售后登记注册信息
        serviceRefunds: {}, // 售后退款信息
        serviceOrderInfo:{}, //订单信息
        costItemData:[], // 费用明细
        userPaymentList:[], // 支付信息
        responsibilityData:[], // 售后责任
        selectedResponsibilityData:[], // 添加的售后责任
        q:{
            srOrderId: null,
            srStatus: null,
        },
        srStatusList:[{label: '全部',value: null},{label: '待审核',value:1},
            {label: '已审核',value:2},{label: '已收货',value:3},
            {label: '已退款',value:4},{label: '已转生产',value:5},
            {label: '已发货',value:6},{label: '客户已收货',value:7},
            {label: '售后完成',value:8},{label: '订单关闭',value:9}],

	},
	methods: {
        loadData: function (data) {
            vm.showList = true;
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "查看";
			vm.serviceRegister = {};
		},
		update: function (event) {
			var srId = getSelectedRow();
			if(srId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(srId)
		},
		/*del: function (event) {
			var srIds = getSelectedRows();
			if(srIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "service/serviceregister/delete",
                    contentType: "application/json",
				    data: JSON.stringify(srIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},*/
		getInfo: function(srId){
            vm.showList = false;
            vm.title = "修改";
			$.get(baseURL + "service/serviceregister/info/"+srId, function(r){
				// todo  异常处理
                vm.serviceRegister = r.serviceRegister;
                vm.serviceOrderInfo = r.serviceOrderInfoVo;
                vm.serviceRefunds = r.serviceRefunds;
                vm.userPaymentList = r.userPaymentList

                var cost={};
                cost.totalAmount = r.serviceOrderInfoVo.totalAmount;
                cost.payableAmount = r.serviceOrderInfoVo.payableAmount;
                cost.discount = r.serviceOrderInfoVo.discount;
                cost.pmtGoods = r.serviceOrderInfoVo.pmtGoods;
                cost.pmtOrder = r.serviceOrderInfoVo.pmtOrder;
                cost.pmtRandom = r.serviceOrderInfoVo.pmtRandom;
                cost.payed = r.serviceOrderInfoVo.payed;
                cost.costFreight = r.serviceOrderInfoVo.costFreight;

                vm.costItemData.push(cost);
                if(r.serviceRegister.srResponsibility){
                    vm.selectedResponsibilityData = JSON.parse(r.serviceRegister.srResponsibility);
                }else {
                    vm.selectedResponsibilityData = [];
                }

            });
            this.initResponsibilityData();
		},
		//获取售后登记所需数据
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData: vm.q
            }).trigger("reloadGrid");
		},

		// 打开售后责任弹框
        openDialogResponsibility: function () {
			vm.initResponsibilityData();
            vm.dialogResponsibilityVisible = true;
        },

		//添加售后责任
        addResponsibility: function () {
        	if(this.$refs.responsibilityTable.selection.length == 0){
                this.$message.error('请添加一个责任');
                return;
			}

            var newObject = jQuery.extend(true, [], this.$refs.responsibilityTable.selection);
            vm.selectedResponsibilityData = newObject;
            vm.dialogResponsibilityVisible = false;

           /* // 请求保存数据库
			var data = {};
            data['srId'] = vm.serviceRegister.srId;
            data['srResponsibility'] = JSON.stringify(newObject);
            $.ajax({
                type: "POST",
                url: baseURL + "service/serviceregister/addResponsibility",
                contentType: "application/json",
                data: JSON.stringify(data),
                success: function (r) {
                    if (r.code === 0) {
                        this.$message({ message: '售后责任添加成功 !',type: 'success' });
                        vm.selectedResponsibilityData = newObject;
                        vm.dialogResponsibilityVisible = false;
                    } else {
                        alert(r.msg);
                    }
                }
            });*/

        },

		// 初始化售后责任列表
		initResponsibilityData: function () {
        	var tempList = [];
			var tempSelf={};
            tempSelf.responsibleParty = '客户自己';
            tempSelf.specificUnit = null;
            tempSelf.reason = null;
            tempSelf.buckleMoney = null;
            var tempLogistics={};
            tempLogistics.responsibleParty = '物流';
            tempLogistics.specificUnit = null;
            tempLogistics.reason = null;
            tempLogistics.buckleMoney = null;
            var tempDesign={};
            tempDesign.responsibleParty = '设计';
            tempDesign.specificUnit = null;
            tempDesign.reason = null;
            tempDesign.buckleMoney = null;
            var tempSalesman={};
            tempSalesman.responsibleParty = '业务员';
            tempSalesman.specificUnit = null;
            tempSalesman.reason = null;
            tempSalesman.buckleMoney = null;
            var tempProducer={};
            tempProducer.responsibleParty = '生产商';
            tempProducer.specificUnit = null;
            tempProducer.reason = null;
            tempProducer.buckleMoney = null;
            var tempOther={};
            tempOther.responsibleParty = '其他';
            tempOther.specificUnit = null;
            tempOther.reason = null;
            tempOther.buckleMoney = null;

            tempList.push(tempSelf);
            tempList.push(tempLogistics);
            tempList.push(tempDesign);
            tempList.push(tempSalesman);
            tempList.push(tempProducer);
            tempList.push(tempOther);

            vm.responsibilityData = tempList;

        },


        // 审核通过
        check : function (status) {
        	var msg = "";
        	if(status == 2){ // 审核通过
                if(vm.selectedResponsibilityData.length == 0){
                    this.$message.error({message: '请先添加售后责任'});
                    return;
                }
                msg = "确定要审核通过吗";
			}

			if(status == 9){ //驳回 关闭订单
                msg = "确定要驳回吗";
			}
			var self = this;
            this.$confirm(msg, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 请求保存数据库
                var data = {};
                data['srId'] = vm.serviceRegister.srId;
                data['srType'] = vm.serviceRegister.srType;
                data['srResponsibility'] = JSON.stringify(vm.selectedResponsibilityData);
                data['srStatus'] = status;
                $.ajax({
                    type: "POST",
                    url: baseURL + "service/serviceregister/check",
                    contentType: "application/json",
                    data: JSON.stringify(data),
                    success: function (r) {
                        if (r.code === 0) {
                            self.$message({ message: '操作成功 !',type: 'success' });
                        } else {
                            alert(r.msg);
                        }
                    }
                });

            }).catch(() => {
                this.$message({type: 'info', message: '已取消审核通过'});
            })
        },

		//驳回
		reject : function () {

        },


	}
});